- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRecursion.rb
36 lines (33 loc) · 937 Bytes
/
Recursion.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val = 0, _next = nil)
# @val = val
# @next = _next
# end
# end
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end
# @param {ListNode} head
# @param {TreeNode} root
# @return {Boolean}
defis_sub_path(head,root)
returnfalseifroot.nil?
returntrueifhead.val == root.val && check_path(head,root)
is_sub_path(head,root.left) || is_sub_path(head,root.right)
end
# @param {ListNode} head
# @param {TreeNode} root
# @return {Boolean}
defcheck_path(head,root)
returntrueifhead.nil?
returnfalseifroot.nil? || head.val != root.val
check_path(head.next,root.left) || check_path(head.next,root.right)
end